<!DOCTYPE html>
<html>

<head>
    <title>点赞消息</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/message.css"/>

    <script>
        $(function () {
            var currentPage = 1;    // 定义全局变量当前页
            var pages;      // 定义总页数
            var travels;    // 定义全局变量游记数组

            //获取作用域中的用户信息
            var user = sessionStorage.getItem("user");
            var json = JSON.parse(user);
            console.log(json);

            $.get("/users/"+json.id+"/sponsors/content",{"currentPage": 1, "pageSize": 16},function (data) {
                console.log(data);
                travels = data.list;
                pages = data.pages;
                console.log(data.pages);
                renderViews(data)
            });

            $(window).scroll(function () {
                // 判断是否触底
                if (isEnd()) {
                    // 判断是否还有下一页
                    if (currentPage < pages) {
                        console.log("应该有下一页");
                        // 当前页+1
                        setTimeout(function () {
                            currentPage++;
                            // 发送ajax请求
                            $.get("/users/"+json.id+"/sponsors/content",{"currentPage": currentPage, "pageSize": 16},function (data) {
                                // 拼接游记数组数据
                                $.merge(travels, data.list);
                                // 渲染页面
                                renderViews({list: travels});
                            });
                        },10);
                    } else {
                        // 否则, 提示已到低
                        $(document).dialog({
                            type: 'notice',
                            infoText: '没有消息了',
                            autoClose: 1000,
                            position: 'bottom'  // center: 居中; bottom: 底部
                        });
                    }
                }
            });
        });

        // 渲染页面的方法
        function renderViews(data) {
            if (data.length == 0) {
                return;
            }
            $("#sponsorName").html("用户");
            $("#userTravel").html("对你的游记:");
            $("#sponsor").html(" 进行了点赞");
            $(".sponsor").renderValues({list: data.list});
        }

        // 定义一个判断屏幕溢出高度 + 屏幕高度 > 网页高度的函数
        function isEnd() {
            return $(window).height() + $(window).scrollTop() >= $(document).height() - 1;
        }

    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
            <span>点赞消息</span>
        </div>
        <div class="col"></div>
    </div>
</div>

<div class="sponsor">
    <ul class="list-group" render-loop="list">
        <div class="sponsorContent">
            <li class="list-group-item d-flex justify-content-between align-items-center">
                <span>
                    <span id="sponsorName"></span>
                    <font color="#3cb371" render-html="list.userName"></font>
                    <span id="userTravel"></span>
                    <font color="#3cb371" render-html="list.travelTitle"></font>
                    <span id="sponsor"></span>
                </span>
            </li>
        </div>
    </ul>
</div>

</body>
</html>